﻿using AsmShaderConverter.AsmShader;
using AsmShaderConverter.HlslShader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsmShaderConverter.AsmFunctions
{
    public class AsmFunctionDot2add : IConvertToHlsl
    {
        readonly string originalLineOfCode;
        private Register destination;
        private Register source1;
        private Register source2;
        private Register source3;

        public Register GetDestinationRegister()
        {
            return this.destination;
        }

        public AsmFunctionDot2add(string lineOfCode)
        {
            this.originalLineOfCode = lineOfCode;

            lineOfCode = lineOfCode.Replace(",", ""); // Remove commas.
            lineOfCode = lineOfCode.Trim(); // Remove dead space.
            var values = lineOfCode.Split(' '); // split

            switch (values.Length)
            {
                case 5:
                    this.destination = new Register(values[1], Swizzle.StandardXYZW);
                    this.source1 = new Register(values[2], destination.swizzle);
                    this.source2 = new Register(values[3], destination.swizzle);
                    this.source3 = new Register(values[4], destination.swizzle);
                    break;
                default:
                    throw new Exception("Function can´t be converted! " + lineOfCode);
            }

        }

        public string ToHLSL(AsmShaderFile asmShader)
        {
            if (destination.swizzle.Length != 1)
                throw new Exception("Länge sollte eigentlich 1 sein");

            // When no swizzle is given we use xy;
            if (source2.swizzle.Length == 0)
                source2.swizzle = new char[] { 'x', 'y' };
            if (source2.swizzle.Length == 0)
                source2.swizzle = new char[] { 'x', 'y' };

            string source1Text = this.source1.ToHLSL(asmShader);
            string source2Text = this.source2.ToHLSL(asmShader);

            if (source1.swizzle.Length == 1) // When we have only one swizzle we must create a float2 first
                source1Text = string.Format("float2({0}, {0})", this.source1.ToHLSL(asmShader));
            if (source2.swizzle.Length == 1) // When we have only one swizzle we must create a float2 first
                source2Text = string.Format("float2({0}, {0})", this.source2.ToHLSL(asmShader));

            if (source1.swizzle.Length > 2 || source2.swizzle.Length > 2)
                throw new Exception("swizzle to long: " + originalLineOfCode);

            return String.Format("{0} = dot({1}, {2}) + {3};", this.destination.ToHLSL(asmShader), source1Text, source2Text, source3.ToHLSL(asmShader));
        }


        public string ToHLSLWitchComment(AsmShaderFile asmShader)
        {
            return this.ToHLSL(asmShader) + "//" + this.originalLineOfCode;
        }
    }
}
